﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.Globalization;
using System.IO;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load( object sender, EventArgs e )
		{
			//using ( ExtRatesEntities context = new ExtRatesEntities() )
			//{
				//Period period = Period.CreatePeriod( 0, DateTime.Now );
				//context.AddToPeriod( period );
				//context.SaveChanges();

				//var record = context.Period.Where( c => c.ID == 1 ).FirstOrDefault();
				//Text = record.DateTime.ToString();
			//}
		}
	}


















    public class FileXML
    {

        private string Date_min, Date_max;
        private int Count;
        public FileXMLItem[] Item = new FileXMLItem[200000];

        public FileXML()
        {
            Date_min = "2099-01-01";
            Date_max = "0000-00-00";
            Count = 0;
            Item = new FileXMLItem[200000];
        }

        public FileXML(string adress)
        {
            Date_min = "2099-01-01";
            Date_max = "0000-00-00";
            Count = 0;
            for (int i = 0; i < 200000; i++)
                Item[i] = new FileXMLItem();
            ReadXML(adress);
        }

        public string get_Date_min()
        {
            return this.Date_min;
        }

        public string get_Date_max()
        {
            return this.Date_max;
        }

        public int get_Count()
        {
            return this.Count;
        }

        private void ReadXML(string Adress)
        {
            string last_teg = "";
            int Count = 0;
            int Count_text = -1;

            XmlTextReader reader = new XmlTextReader(Adress);

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    //is element
                    case XmlNodeType.Element:
                        if (reader.Name != "Rates" && reader.Name != "ArrayOfRates")
                        {
                            Count_text++;
                            this.Item[Count].Name[Count_text] = reader.Name;
                        }
                        last_teg = reader.Name;
                        break;

                    case XmlNodeType.Text:
                        if (last_teg == "DateTime")
                        {
                            this.Item[Count].Date = reader.Value;
                            string Contain = reader.Value;
                            Contain = Contain.Substring(0, 10);
                            if (String.Compare(Contain, this.Date_max) > 0)
                                this.Date_max = Contain;
                            if (String.Compare(Contain, this.Date_min) < 0)
                                this.Date_min = Contain;
                        }
                        this.Item[Count].Value[Count_text] = reader.Value;
                        break;

                    //end file
                    case XmlNodeType.EndElement:
                        if (reader.Name == "Rates")
                        {
                            this.Item[Count].Count = Count_text + 1;
                            Count_text = -1;
                            Count++;
                        }
                        break;
                }
            }

            this.Count = Count;

        }

        public string CreateXML(string Adress, DateTime Date_1, DateTime Date_2)
        {
            string Date_format = "yyyy_MM_dd";
            string file_new_path = Adress + "_" + Date_1.ToString(Date_format).ToString() + "_" + Date_2.ToString(Date_format).ToString() + ".xml";
            XmlTextWriter textWriter = new XmlTextWriter(file_new_path, null);

            textWriter.WriteStartDocument();
            textWriter.Formatting = Formatting.Indented;
            textWriter.Indentation = 4;
            textWriter.WriteStartElement("ArrayOfRates");
            for (int i = 0; i < this.Count; i++)
                if (Convert.ToDateTime(this.Item[i].Date) >= Date_1 && Convert.ToDateTime(this.Item[i].Date) < Date_2)
                {
                    textWriter.WriteStartElement("Rates");
                    textWriter.WriteStartElement("CurrencyRates");

                    bool Open = true;
                    for (int j = 1; j < this.Item[i].Count; j++)
                    {
                        if (this.Item[i].Name[j] == "Rate" && Open)
                        {
                            textWriter.WriteStartElement(this.Item[i].Name[j]);
                            Open = false;
                        }
                        else
                            if (this.Item[i].Name[j] == "Rate")
                            {
                                textWriter.WriteEndElement();
                                textWriter.WriteStartElement(this.Item[i].Name[j]);
                            }
                            else
                                if (this.Item[i].Name[j] == "DateTime")
                                {
                                    textWriter.WriteEndElement();
                                    textWriter.WriteEndElement();
                                    textWriter.WriteStartElement(this.Item[i].Name[j]);
                                    textWriter.WriteString(this.Item[i].Value[j]);
                                    textWriter.WriteEndElement();
                                }
                                else
                                {
                                    textWriter.WriteStartElement(this.Item[i].Name[j]);
                                    textWriter.WriteString(this.Item[i].Value[j]);
                                    textWriter.WriteEndElement();
                                }
                    }

                    textWriter.WriteEndElement();
                }
            textWriter.WriteEndElement();
            textWriter.WriteEndDocument();
            textWriter.Close();

            return file_new_path;
        }

        public string sAddToDb(string Adress, DateTime Date_1, DateTime Date_2)
        {
            string Date_format = "yyyy_MM_dd";
            string file_new_path = Adress + "_" + Date_1.ToString(Date_format).ToString() + "_" + Date_2.ToString(Date_format).ToString() + ".xml";

            //using (ExtRatesEntities context = new ExtRatesEntities())
            {
                //Period period = Period.CreatePeriod( 0, DateTime.Now );
                //context.AddToPeriod( period );
                //context.SaveChanges();

                //var record = context.Period.Where(c => c.ID == 1).FirstOrDefault();
                //Text = record.DateTime.ToString();
            }


            /*XmlTextWriter textWriter = new XmlTextWriter(file_new_path, null);

            textWriter.WriteStartDocument();
            textWriter.Formatting = Formatting.Indented;
            textWriter.Indentation = 4;
            textWriter.WriteStartElement("ArrayOfRates");
            for (int i = 0; i < this.Count; i++)
                if (Convert.ToDateTime(this.Item[i].Date) >= Date_1 && Convert.ToDateTime(this.Item[i].Date) < Date_2)
                {
                    textWriter.WriteStartElement("Rates");
                    textWriter.WriteStartElement("CurrencyRates");

                    bool Open = true;
                    for (int j = 1; j < this.Item[i].Count; j++)
                    {
                        if (this.Item[i].Name[j] == "Rate" && Open)
                        {
                            textWriter.WriteStartElement(this.Item[i].Name[j]);
                            Open = false;
                        }
                        else
                            if (this.Item[i].Name[j] == "Rate")
                            {
                                textWriter.WriteEndElement();
                                textWriter.WriteStartElement(this.Item[i].Name[j]);
                            }
                            else
                                if (this.Item[i].Name[j] == "DateTime")
                                {
                                    textWriter.WriteEndElement();
                                    textWriter.WriteEndElement();
                                    textWriter.WriteStartElement(this.Item[i].Name[j]);
                                    textWriter.WriteString(this.Item[i].Value[j]);
                                    textWriter.WriteEndElement();
                                }
                                else
                                {
                                    textWriter.WriteStartElement(this.Item[i].Name[j]);
                                    textWriter.WriteString(this.Item[i].Value[j]);
                                    textWriter.WriteEndElement();
                                }
                    }

                    textWriter.WriteEndElement();
                }
            textWriter.WriteEndElement();
            textWriter.WriteEndDocument();
            textWriter.Close();*/

            return file_new_path;
        }

    };



    public class FileXMLItem
    {

        public int Count;
        public string Date;
        public string[] Name = new string[150];
        public string[] Value = new string[150];

        public FileXMLItem()
        {
            Count = 0;
            Date = "";
            for (int i = 0; i < 150; i++)
            {
                Name[i] = "";
                Value[i] = "";
            }
        }

    };



    public class TwoStrings
    {
        public string s1;
        public string s2;
        public string s3;

        public TwoStrings()
        {
            s1 = "";
            s2 = "";
            s3 = "";
        }

        public TwoStrings(TwoStrings x)
        {
            s1 = x.s1;
            s2 = x.s2;
            s3 = x.s3;
        }
    }



}